package hyo.hun.lifemanager.connect;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.Events;

class AsyncLoadEventArange extends AsyncTask<Void, Void, Void> {

  private final CalendarConnect calendarConnect;
  private Context context;
  private final ProgressDialog dialog;
  private final String calendarId;
  private com.google.api.services.calendar.Calendar client;

  AsyncLoadEventArange(CalendarConnect calendarConnect, Context context, String calendarId) {
    this.calendarConnect = calendarConnect;
    this.context = context;
    this.client = CalendarConnect.getClient();
    this.calendarId = calendarId;
    dialog = new ProgressDialog(context);
  }

  @Override
  protected void onPreExecute() {
    dialog.setMessage("Loading Events...");
    dialog.show();
  }

  @Override
  protected Void doInBackground(Void... arg0) {
    try {
      // client.calendars().delete(calendarId).execute();
      // calendarSample.calendars.remove(calendarIndex);

      Events events;
      String pageToken = null;
      do {

        Calendar firstDateCal = Calendar.getInstance();
        firstDateCal.set(Calendar.YEAR, 2013);
        firstDateCal.set(Calendar.MONTH, 1 - 1);
        firstDateCal.set(Calendar.DATE, 1);
        firstDateCal.set(Calendar.HOUR_OF_DAY, 0);
        firstDateCal.set(Calendar.MINUTE, 0);
        firstDateCal.set(Calendar.SECOND, 0);

        Calendar lastDateCal = Calendar.getInstance();
        lastDateCal.set(Calendar.YEAR, 2014);
        lastDateCal.set(Calendar.MONTH, 3 - 1);
        int lastDateOfMonth = lastDateCal.getActualMaximum(Calendar.DATE);
        lastDateCal.set(Calendar.DATE, lastDateOfMonth);
        firstDateCal.set(Calendar.HOUR_OF_DAY, 0);
        firstDateCal.set(Calendar.MINUTE, 0);
        firstDateCal.set(Calendar.SECOND, 0);
        // DateTime startTime = new DateTime(new Date(), TimeZone.getDefault());
        events =
            client
                .events()
                .list(calendarId)
                .setTimeMin(
                    new DateTime(new Date(firstDateCal.getTimeInMillis()), TimeZone.getDefault()))
                .setTimeMax(
                    new DateTime(new Date(lastDateCal.getTimeInMillis()), TimeZone.getDefault()))
                .setPageToken(pageToken).execute();
        List<Event> items = events.getItems();
        for (Event event : items) 
        {
        	EventInfo info = new EventInfo(event.getId(), event.getSummary());
        	ArrayList<EventInfo> listEvents = new ArrayList<EventInfo>();
			listEvents.add(info);
			calendarConnect.setEventList(listEvents);
        	//calendarSample.events.add(info);
        }
        pageToken = events.getNextPageToken();
      } while (pageToken != null);

      /*
       * Events events; String pageToken = null; do { events =
       * client.events().list(calendarId).setPageToken(pageToken).execute(); List<Event> items =
       * events.getItems(); for (Event event : items) {
       * 
       * Calendar firstDateCal = Calendar.getInstance(); firstDateCal.set(Calendar.YEAR, 2014);
       * firstDateCal.set(Calendar.MONTH, 1 - 1); firstDateCal.set(Calendar.DATE, 1);
       * firstDateCal.set(Calendar.HOUR_OF_DAY, 0); firstDateCal.set(Calendar.MINUTE, 0);
       * firstDateCal.set(Calendar.SECOND, 0);
       * 
       * Calendar lastDateCal = Calendar.getInstance(); lastDateCal.set(Calendar.YEAR, 2014);
       * lastDateCal.set(Calendar.MONTH, 1 - 1); int lastDateOfMonth =
       * lastDateCal.getActualMaximum(Calendar.DATE); lastDateCal.set(Calendar.DATE,
       * lastDateOfMonth); firstDateCal.set(Calendar.HOUR_OF_DAY, 0);
       * firstDateCal.set(Calendar.MINUTE, 0); firstDateCal.set(Calendar.SECOND, 0);
       * 
       * // Date firstDate = new Date(2014 - 1900, 1, 1, 0, 0, 0); // Date lastDate = new Date(2014
       * - 1900, 1, lastDateOfMonth, 0, 0, 0); // // DateTime dateTimeFirstDate = new
       * DateTime(firstDate); // DateTime dateTimeLastDate = new DateTime(lastDate);
       * 
       * if (firstDateCal.getTimeInMillis() < event.getStart().getDateTime().getValue() &&
       * event.getStart().getDateTime().getValue() < lastDateCal.getTimeInMillis()) {
       * 
       * EventInfo info = new EventInfo(event.getId(), event.getSummary());
       * calendarSample.events.add(info);
       * 
       * Log.e("11111111", new Date(firstDateCal.getTimeInMillis()) + " < " +
       * event.getStart().getDateTime() + event.getStart().getDateTime() + " < " + new
       * Date(lastDateCal.getTimeInMillis())); } else { Log.e("2222222", "not found"); //
       * Log.e("22222222", // event.getStart().getDateTime().getValue() + " < " +
       * cal.getTimeInMillis()); // Log.e("33333333", dateTime.toString()); // Log.e("44444444",
       * event.getStart().getDateTime() + ""); } } pageToken = events.getNextPageToken(); } while
       * (pageToken != null);
       */

    } catch (IOException e) {
      //calendarSample.handleGoogleException(e);
    } finally {
      //calendarSample.onRequestCompleted();
    }
    return null;
  }

  @Override
  protected void onPostExecute(Void result) {
    dialog.dismiss();
    //calendarSample.refreshEvents();
  }
}
